#!/bin/env ruby
# encoding: utf-8
require_relative '../spec/spec_helper'
require_relative '../spec/commons/user_auth'


#TODO to fix email body tests
describe 'Improvements collecting email template page' do
  before {
    
  }

  #open the collecting page
  context "when logged in" do
    it "then he can work with template email page" do
      goto 'http://pulse.7bits.it/en/login-page'
      sign_in('adm.company2@mail.ru','123qwe+')
      goto 'http://pulse.7bits.it/en/us/admin/collecting'
      check_collecting_page()
      #STDERR.puts ('Now URL is: ' + browser.url())#??
   end
  end

  #subject couldn't be empty
  context "when subject is changed" do
    it "then subject couldn't be empty" do
      goto 'http://pulse.7bits.it/en/us/admin/collecting'
      text_field(name: 'subject').focus()
      text_field(name: 'subject').clear()      
      text_field(name: 'subject').set ""
     
      button(text: "Save").click()
      #form(name: 'collectingEmailSettingForm').submit
      span(text: "Subject can't be blank").wait_until_present       
    end
  end
  
  #subject couldn't be more then 255 characters
  context "when subject is changed" do
    it "then subject couldn't be more then 255 characters" do   
      goto 'http://pulse.7bits.it/en/us/admin/collecting'    
      upstr='Q'
      256.times{|i| upstr+='a'}
      text_field(name: 'subject').set  upstr
      button(text: 'Save').click() 
      span(text: "Subject should contain no more than 255 symbols").wait_until_present          
     end
  end

  #mail body is empty
  context "when mail is changed" do
    it "then mail body couldn't be empty" do      
      #iframe(class: 'cke_wysiwyg_frame cke_reset').focus
      #iframe(class: 'cke_wysiwyg_frame cke_reset').focus()
      #iframe(class: 'cke_wysiwyg_frame cke_reset').html().body().clear() 
      #text_field(id: 'js-ckeditor').clear
      
      # iframe(class: 'cke_wysiwyg_frame cke_reset').send_keys "" 
      # iframe(:index, 0).locate     
      # button(type: 'submit').click
      # span(text: "Body can't be blank").wait_until_present
    end
  end
 
  #TODO непонятно, как полностью очистить содержимое
  # ruby или browser или еще кто не понимает как добавить 10000 знаков. 
  #mail body can not contain more than 10000 characters
  context "when email maxlength exceeded" do
    it "then email should contain less than 10000 symbols" do
      goto 'http://pulse.7bits.it/en/us/admin/collecting' 
      upstr='Q'
      #6001.times{|i| upstr+='a'  }   
      #browser.element(:xpath, 'html/body').focus

      #elements=browser.element(:xpath, 'html/body').divs.
      #elements.each{|d| d.text.clear()}
      #browser.div(:xpath,".//*[@id='cke_1_contents']").clear
      iframe(class: 'cke_wysiwyg_frame cke_reset').send_keys upstr
      iframe(:index, 0).locate 
      button(type: 'submit').click()
      #span(text: "Body should contain no more than 10000 symbols").wait_until_present
       
    end
  end

  #user logged out
  context "when logged out" do
    it "then he can't work with template email page"  do
      user_logout()
    end
  end

end